﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;
using System.Drawing;
public partial class Manager_Revenue_Revenue : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminUserID"] == null)
        {
            Response.Write("<script language=javascript>alert('Time is out.');window.open('../Default.aspx','_top');</script>");
        }
        if (!IsPostBack)
        {
            BindYearOrder();
            BindData();
         
        }
       
    }

    private void BindYearOrder()
    {
        clsWebShop cls=new clsWebShop();
        DataSet ds = cls.GetData("SELECT left(OrderDate,4)Year FROM [OrderPD] Group By left(OrderDate,4) order by left(OrderDate,4) desc");
        ddlYear.DataSource = ds;
        ddlYear.DataValueField = "Year";
        ddlYear.DataTextField = "Year";
        ddlYear.DataBind();
    }
  
    private void BindData()
    {
        clsCommon clsCom = new clsCommon();
        DataTable dt = new DataTable();
        dt = Get_CalOrderDetail();
        string cssHeader = "TableHeader";
        string cssFooter = "TableFooterStyle";
        string cssHover = "TableRowHoverStyle";

        string cssSaleInHeader = "#6aeaff";
        string cssSaleOutHeader = "#00d5ff";

        string cssSaleIn = "#8dffff";
        string cssSaleOut = "#33ffff";


        string cssSubFooter = "TableSubTotalStyle";
        if (dt.Rows.Count > 0)
        {
            //Dong 1
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            cell.Text = "Tháng";
            //cell.RowSpan = 2;
            cell.CssClass = cssHeader;
            cell.Wrap = false;
            cell.Width = Unit.Percentage(2);
            row.Cells.Add(cell);


            for (int i = 1; i <= 12; i++)
            {
                cell = new TableCell();
                cell.Text = i.ToString();
                cell.ColumnSpan = 2;
                cell.CssClass = cssHeader;
                row.Cells.Add(cell);            

                tblMaster.Rows.Add(row);              
            }

            
            //Dong 2

            //int cDay = System.DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
            for (int d = 0; d <= 31; d++)
            {
                
                string cssRow = "TableRowStyle";
                if (d % 2 == 0)
                {
                    cssRow = "TableAlternatingRowStyle";
                }
                row = new TableRow();
                if (d != 0)
                {
                    cell = new TableCell();
                    cell.Text = d.ToString();
                    cell.CssClass = cssRow;
                    cell.HorizontalAlign = HorizontalAlign.Center;
                    cell.RowSpan = 2;
                    row.Cells.Add(cell);
                    tblMaster.Rows.Add(row);
                }
                else
                {
                    cell = new TableCell();
                    cell.Text = "Ngày";
                    cell.CssClass = cssHeader;
                    cell.Wrap = false;
                    cell.Width = Unit.Percentage(2);
                    row.Cells.Add(cell);
                }
                for (int m= 1; m <= 12; m++)
                {
                    if (d == 0)
                    {                       
                        cell = new TableCell();
                        cell.Text = "Mua vào";
                        cell.CssClass = cssHeader;
                        //cell.BackColor = Color.FromName(cssSaleInHeader);
                        cell.Wrap = false;
                        row.Cells.Add(cell);
                        //
                        cell = new TableCell();
                        cell.Text = "Bán ra";
                        cell.CssClass = cssHeader;
                       // cell.BackColor = Color.FromName(cssSaleOutHeader);
                        cell.Wrap = false;
                        row.Cells.Add(cell);
                        
                        tblMaster.Rows.Add(row);
                    }
                    else
                    {
                        string sm = m < 10 ? "0" + m.ToString() : m.ToString();
                        string sd = d < 10 ? "0" + d.ToString() : d.ToString();
                        DataRow[] dr = dt.Select("OrderDate='"+ddlYear.SelectedValue+"" + sm + sd + "'");
                       
                        if (dr.Length > 0)
                        {
                            string sBuyIn=dr[0]["BuyIn"].ToString();
                            string sSaleOut=dr[0]["SaleOut"].ToString();
                            string ssMonth = m < 10 ? "0" + m.ToString() : m.ToString();
                            string ssDay = d < 10 ? "0" + d.ToString() : d.ToString();
                            cell = new TableCell();
                            cell.Text = clsCom.FormatMoney(sBuyIn).ToString();
                            cell.Attributes.Add("onclick", "fnOpenOrderDetail(" + ddlYear.SelectedValue + ssMonth + ssDay + ",'in')");
                            cell.Attributes.Add("onmouseout", "this.className='" + cssRow + "'");
                            cell.Attributes.Add("onmouseover", "this.className='" + cssHover + "'");
                            cell.Attributes.CssStyle.Add("cursor", "pointer");
                            cell.HorizontalAlign = HorizontalAlign.Right;
                            cell.CssClass = cssRow;
                           // cell.BackColor = Color.FromName(cssSaleIn);
                            row.Cells.Add(cell);
                            cell = new TableCell();
                            cell.Text = clsCom.FormatMoney(sSaleOut).ToString();

                      
                            cell.Attributes.Add("onclick", "fnOpenOrderDetail(" + ddlYear.SelectedValue + ssMonth + ssDay + ",'out')");
                            cell.Attributes.Add("onmouseout", "this.className='" + cssRow + "'");
                            cell.Attributes.Add("onmouseover", "this.className='" + cssHover + "'");
                            cell.Attributes.CssStyle.Add("cursor", "pointer");

                            cell.HorizontalAlign = HorizontalAlign.Right;
                            cell.CssClass = cssRow;
                            
                           // cell.BackColor = Color.FromName(cssSaleOut);
                            row.Cells.Add(cell);
                        }
                        else
                        {
                            cell = new TableCell();
                            cell.Text = "0";
                            cell.CssClass = cssRow;
                            //cell.BackColor = Color.FromName(cssSaleIn);
                            cell.HorizontalAlign = HorizontalAlign.Right;
                            row.Cells.Add(cell);
                            cell = new TableCell();
                            cell.HorizontalAlign = HorizontalAlign.Right;
                            cell.Text = "0";
                            cell.CssClass = cssRow;
                            //cell.BackColor = Color.FromName(cssSaleOut);
                            row.Cells.Add(cell);
                        }                     

                    }                 
                   
                }
                tblMaster.Rows.Add(row);

                //Footer
                if (d != 0)
                {
                    row = new TableRow();
                    //sub footer
                    int cellindex = 1;
                    for (int t =1; t <=12; t++)
                    {
                        decimal dRemailPrice = 0;
                        cell = new TableCell();
                       
                        int rowindex = tblMaster.Rows.Count-1; //Lay rowsub dang tinh tong                         

                        string sBuyIn = tblMaster.Rows[rowindex].Cells[cellindex].Text.ToString().Replace(",", "");
                        string sSaleOut = tblMaster.Rows[rowindex].Cells[cellindex+1].Text.ToString().Replace(",", "");
                        dRemailPrice = decimal.Parse(sSaleOut) - decimal.Parse(sBuyIn);
                        if (dRemailPrice < 0)
                        {
                            cell.BackColor = Color.Yellow;
                        }
                     
                        cell.ColumnSpan = 2;
                        cell.HorizontalAlign = HorizontalAlign.Right;
                        cell.Text = clsCom.FormatMoney(dRemailPrice.ToString());
                        
                        cellindex += 2; //Tang len 2 columnSpan=2   
                        cell.CssClass = cssSubFooter;
                        cell.Attributes.CssStyle.Add("cursor", "pointer");
                        row.Cells.Add(cell);
                    }
                    tblMaster.Rows.Add(row);
                }
            }


            row = new TableRow();
            ///Footer
            //cot so ngay
            cell = new TableCell();
            cell.Text = "Tổng Cộng";
            cell.CssClass = cssFooter;
            cell.Wrap = false;
            row.Cells.Add(cell);


            //
            for (int t = 0; t < 12; t++)
            {
                decimal dTotal = 0;
                for (int i = 3; i < 64; i +=2)
                {
                    dTotal += decimal.Parse(tblMaster.Rows[i].Cells[t].Text);
                }
    
                cell = new TableCell();
                cell.ColumnSpan = 2;
                cell.HorizontalAlign = HorizontalAlign.Right;
                cell.Text = clsCom.FormatMoney(dTotal.ToString());
                cell.CssClass = cssFooter;
                row.Cells.Add(cell);
            }
            tblMaster.Rows.Add(row);

            tblMaster.Rows.Add(row);
            tblMaster.CellPadding = 2;
            tblMaster.CellSpacing = 0;
            tblMaster.BorderWidth = Unit.Pixel(1);
            tblMaster.Width = Unit.Percentage(100);
        }
    }

    private DataTable Get_CalOrderDetail()
    {
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("Year",ddlYear.SelectedValue);

        clsWebShop cls = new clsWebShop();
        DataSet ds = null;
        ds = cls.Get_CalOrderDetail(msg.GetXmlStr);
        return ds.Tables[0];
    }

    protected DateTime LastDateofMonth(DateTime dt)
    {
        dt = new DateTime(dt.Year, dt.Month, 1); // Find out the first day of month
        dt = dt.AddMonths(1).AddDays(-1); // Last Day of Month will be previous day of next month's first date

        return dt;
    }
    protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindData();
    }
}
